package net.gq.logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.gq.logistics.mapper.BaseDataMapper;
import net.gq.logistics.pojo.BaseData;
import net.gq.logistics.pojo.BaseDataExample;
import net.gq.logistics.pojo.ResultData;
import net.gq.logistics.service.BaseDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.ws.Response;
import java.util.List;

/**
 * @author ASUS
 * @date 2020/5/25
 */
@RequestMapping("/baseData")
@Controller("baseDataController")
public class BaseDataController {

    @Resource(name = "baseDataService")
    private BaseDataService baseDataService;

    @Resource(name = "resultData")
    private ResultData rs;


    @RequestMapping("/baseDataPage")
    public String showBaseData() {

        return "baseDataPage";
    }

    /** 基础数据的分页
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<BaseData> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String keyword) {

        BaseDataExample example = new BaseDataExample();

        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotBlank(keyword)) {

            //增加限制条件
            BaseDataExample.Criteria criteria = example.createCriteria();
            criteria.andBaseNameLike("%" + keyword + "%");

            //根据所属类别查询
            BaseDataExample.Criteria criteria1 = example.createCriteria();
            criteria1.andParentNameLike("%" + keyword + "%");

            example.or(criteria1);
        }

        List<BaseData> baseDatas = baseDataService.selectByExample(example);


        //返回分页数据
        return new PageInfo<BaseData>(baseDatas);

    }

    /** 基础数据的批量删除
     * @param str
     * @return
     */
    @RequestMapping("/deleteAll")
    @ResponseBody
    public ResultData deleteAll(String str) {

        int result = 0;

        if (StringUtils.isNotBlank(str)) {
            //对字符串进行拆分

            String[] baseIds = str.split(",");

            for (String baseId : baseIds) {

                result = baseDataService.deleteByPrimaryKey(Long.valueOf(baseId));
            }

            if (result > 0) {
                rs = ResultData.createRs(1, "删除成功!");
            } else {
                rs = ResultData.createRs(0, "删除失败");
            }
        }

        return rs;
    }

    /** 依据id 删除特定数据
     * @param baseId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResultData delete(Long baseId){



         /*
         *  删除之前检查对应的权限id是否还有子权限

            查询parentId = 要删除的权限Id,
         * */

        BaseDataExample example=new BaseDataExample();
        BaseDataExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(baseId);



        List<BaseData> children = baseDataService.selectByExample(example);

        if (children.size() > 0) {
            rs=ResultData.createRs(0,"此数据存在子数据,不能删除!");
            return rs;
        }

        //开始删除
        int result = baseDataService.deleteByPrimaryKey(baseId);



        if (result > 0) {
            rs=ResultData.createRs(1,"删除成功!");
        } else {
            rs=ResultData.createRs(0,"删除失败!");
        }

        //回写json
        return rs;
    }



    @RequestMapping("/edit")
    public String edit(Model model,Long baseId){

        System.out.println(baseId);
        if (baseId != null) {
            //查询特定数据,进入编辑页面

            BaseData baseData = baseDataService.selectByPrimaryKey(baseId);

            System.out.println(baseData);
            model.addAttribute("baseData",baseData);

        }

        //查询出所有基础数据中parent_id=null类别数据
        BaseDataExample example=new BaseDataExample();
        BaseDataExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdIsNull();
        List<BaseData> parents = baseDataService.selectByExample(example);


        model.addAttribute("parents",parents);



        return "baseDataEdit";
    }


    @RequestMapping("/insert")
    @ResponseBody
    public ResultData insert(BaseData baseData){


        int result = baseDataService.insert(baseData);
        if (result > 0) {
            rs=ResultData.createRs(1,"添加成功!");
        } else {
            rs=ResultData.createRs(0,"添加失败!");
        }


        return rs;

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResultData update(BaseData baseData){


        int result = baseDataService.updateByPrimaryKeySelective(baseData);
        if (result >0) {
            rs=ResultData.createRs(1,"修改成功!");
        } else {
            rs=ResultData.createRs(0,"修改失败!");
        }

        return rs;
    }

}
